<template>
  <div>
    <ol class="breadcrumb">
      <li class="breadcrumb-item">系统管理</li>
      <li class="breadcrumb-item">
        <router-link to="/permission">菜单、权限</router-link>
      </li>
      <li class="breadcrumb-item active">新增权限</li>
    </ol>
    <form autocomplete="off" @submit.prevent="submit" class="row mx-0">
      <div class="col-lg-7">
        <div class="card mb-3 p-3">
          <div class="mb-3">
            <h4 class="card-title">必填项</h4>
            <span class="text-muted">These fileds are <code>required</code>.</span>
          </div>
          <div class="row">
            <label class="form-group col-md-6" v-styling="'name'">名称
              <input name="name" v-model="permission.name" v-validate="'required'" class="form-control" title="名称" placeholder="菜单、权限名称">
              <span class="form-control-feedback">{{ errors.first('name') }}</span>
            </label>
            <label class="form-group col-md-6" v-styling="'code'">编码
              <input name="code" v-model="permission.code" v-validate="'required'" class="form-control" title="编码" placeholder="菜单、权限编码">
              <span class="form-control-feedback">{{ errors.first('code') }}</span>
            </label>
          </div>
          <label class="form-group">路径
            <input name="path" v-model="permission.path" class="form-control" title="路径" placeholder="菜单访问路径">
          </label>
          <div class="form-group">
            <label>图标</label>
            <div>
              <span class="align-middle fa-2x text-theme-pink" :class="permission.icon ? permission.icon : 'fa fa-fonticons'"></span>
              <span class="align-middle mx-2">{{permission.icon || '您可以为' + (permission.type == 1 ? '菜单' : '权限') + '指定一个图标:'}}</span>
              <button class="btn btn-sm btn-outline-info" data-toggle="modal" data-target="#icon-modal" title="选择图标" type="button">选择图标</button>
            </div>
          </div>
          <div class="row">
            <label class="form-group col-md-6">状态
              <dict code="status" v-model="permission.status" class="form-control"></dict>
            </label>
            <label class="form-group col-md-6">排序
              <input name="priority" v-model="permission.priority" class="form-control" type="number" max="99999" placeholder="排序越大越靠前">
            </label>
          </div>
          <label class="form-group">描述
            <textarea name="description" v-model="permission.description" class="form-control" placeholder="菜单、权限描述" rows="2"></textarea>
          </label>
        </div>
      </div>
      <div class="col-lg-5">
        <div class="card mb-3 p-3">
          <div class="mb-3">
            <h4 class="card-title">选填项</h4>
            <span class="text-muted">These fileds are <code class="text-info">optional</code></span>
          </div>
          <div class="card-block">
            <label class="text-muted">指定父级权限（菜单）</label>
            <jstree :treenodes="treenodes" v-model="permission.parent"></jstree>
          </div>
        </div>
      </div>

      <div class="modal fade" id="icon-modal">
        <div class="modal-dialog modal-lg">
          <div class="modal-content">
            <div class="modal-header">
              <h4 class="modal-title">选择图标</h4>
              <a class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></a>
            </div>
            <div class="modal-body p-0">
              <p>One fine body ....</p>
            </div>
            <div class="modal-footer">
              <a class="btn btn-secondary" data-dismiss="modal">Close</a>
            </div>
          </div>
        </div>
      </div>
      <div class="col-12">
        <button class="btn btn-outline-info rounded" :disabled="errors.any()">提交</button>
        <router-link class="btn btn-outline-info rounded" to="/permission">返回</router-link>
      </div>
    </form>
  </div>
</template>

<script>
  import jstree from '../../widgets/jstree.vue'
  import {Permission} from '../../resources'
  import utils from '../../misc/utils'
  import dict from '../../widgets/dict.vue'
  import $ from 'jquery'

  export default {
    data: () => ({
      permission: {
        description: '',
        parentId: '',
        path: '',
        icon: '',
        status: 1
      },
      treenodes: []
    }),
    beforeRouteEnter (to, from, next) {
      Permission.jstree().then(tree => {
        next(vm => {
          vm.treenodes = tree.data
          vm.permission.parent = to.query.parent || ''
          vm.$nextTick(() => vm.$emit('loaded') )
        })
      })
    },
    methods: {
      submit () {
        this.$validator.validateAll().then(success => {
          if (!success) return
          Permission.save(this.permission).then(response => {
            utils.showResponse(response, () => this.$router.back())
          })
        })
      }
    },
    mounted () {
      var form = document.forms['permission']
      var $modal = $('#icon-modal', form)
      var that = this

      $modal.on('show.bs.modal', function () {
        $modal.find('.modal-body')
            .load('/static/html/ui.font-awesome-icons.html')
            .on('dblclick', '.fa-hover', function (e) {
              that.permission.icon = $(this).find('i').attr('class')
              $modal.modal('hide')
            })
      })
    },
    components: {jstree, dict}
  }
</script>